const request = require('../../utils/request.js');
//获取应用实例
const app = getApp()
var timer;
var timer2;
Page({
  data: {
    isOpen: false,
    showModal: false,
    newTip: false
  },
  onLoad: function (options) {
    var that = this;
    //判断是否是通过扫码进入页面
    if (options.id) {
      clearInterval(timer2);
      timer2 = null;
      // 获取本地存储中的token
      timer2 = setInterval(function () {
        wx.getStorage({
          key: 'token',
          success: function (res) {//有该数据
            clearInterval(timer2);
            that.setData({
              token: res.data
            })
            var tempToken = res.data;
            var postData = {};
            postData.session_id = tempToken;
            postData.qrcode = options.id;
            postData.money = 99;
            console.log(postData)
            //扫码请求接口
            that.mainIndex(postData);

          },
          fail: function (res) {
            //调用授权方法-设置token
            app.getUserInfo();
          }
        })
      }, 100)
    }
  },
  onShow: function () {
    var that = this;
    that.setData({
      showModal: false
    })
    that.getToken();
  },
  getToken: function () {
    var that = this;
    clearInterval(timer);
    timer = null;
    // 获取本地存储中的token
    timer = setInterval(function () {
      wx.getStorage({
        key: 'token',
        success: function (res) {//有该数据
          clearInterval(timer);
          console.log(res.data)
          that.setData({
            token: res.data
          })
          var tempToken = res.data;
          //判断是不是新用户
          request.getNewUser({
            "session_id": tempToken
          },
            (res) => {
              console.log(res);
              if (res.data.code == "0") {
                that.setData({
                  newTip: true
                })
              }
            }
          )

          //判断有没有开门中
          request.getIsOpenDoor({
            'session_id': tempToken
          },
            (res) => {
              console.log(res)
              if (res.data.status == "success") {
                that.setData({
                  isOpen: true
                })
              } else {
                that.setData({
                  isOpen: false
                })
              }
            })
          app.getUserInfo();
        },
        fail: function (res) {
          //调用授权方法-设置token
          app.getUserInfo();
        }
      })
    }, 100)
  },
  //这块利用定时器的方法每隔100毫秒登陆授权一次
  //
  goOpen: function () {
    wx.navigateTo({
      url: '../openBookcase/onOpen/onOpen?caseId=1',
    })
  },


  //跳转到附近书柜列表
  goMenu: function (e) {
    wx.navigateTo({
      url: '../index/index-nearby/index-nearby',
    })
  },
  //跳转到我的页面
  goMy: function (e) {
    wx.navigateTo({
      url: '../my/my',
    })
  },

  //扫码
  goScanCode: function () {
    console.log(getCurrentPages())
    var that = this;

    wx.scanCode({
      success: (res) => {
        console.log(res)
        console.log(res.path.slice(21))
        that.setData({
          code: res.path.slice(21)
        })
        var tempCode = res.path.slice(21);

        if (tempCode && that.data.token) {
          var postData = {};
          postData.session_id = that.data.token;
          postData.qrcode = tempCode;
          postData.money = 99;
          console.log(postData)
          //扫码请求接口
          that.mainIndex(postData);
        }
      }
    })
  },

  mainIndex: function (param_data) {
    var that = this;
    request.addDeposit(
      param_data,
      (res) => {
        console.log(res.data);

        if (res.data.code == "10003" || res.data.code == "10004") {//未交押金
          var order_no = res.data.order_no;
          wx.showModal({
            title: '缴纳押金',
            content: '立即缴纳押金，开启智慧阅读生活',
            cancelText: "回首页",
            confirmText: "确定",
            confirmColor: "#FF0000",
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../my/my-wallet/my-addDeposit/my-addDeposit?order_no=' + order_no,
                })
              }
            }
          })
        } else if (res.data.code == "0") {//已交押金
          wx.navigateTo({
            url: '../openBookcase/openBookcase',
          })

        } else if (res.data.code == "10002") {//未支付的订单
          wx.showModal({
            title: '',
            content: res.data.msg,
            confirmColor: "#FF0000",
            confirmText: "去支付",
            cancelText: "返回",
            success: function () {

              wx.navigateTo({
                url: '../my/my-loanRecord/my-loanRecord?curId=2',
              })
            }
          })
        } else if (res.data.code == "10008") {//没有获取到手机号

          wx.showModal({
            title: '',
            content: "请到个人中心绑定手机号后再借阅图书哦~",
            confirmColor: "#FF0000",
            confirmText: "去绑定",
            cancelText: "取消",
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../my/my',
                })
              }
            }
          })


        } else {//10001请求失败,10005发送失败，10007他人正在使用中
          wx.showModal({
            title: '',
            content: res.data.msg,
            showCancel: false,
            confirmColor: "#FF0000"
          })
        }
      },
    )
  },
  //跳转到反馈
  goContact: function (e) {
    var that = this;
    that.setData({
      showModal: true
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: '启阅读吧',
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  goUseTip: function () {
    wx.navigateTo({
      url: '../my/my-useTip/my-useTip',
    })
  },
  goFaq: function () {
    wx.navigateTo({
      url: '../my/my-faq/my-faq',
    })
  },
  goRepaire: function () {
    wx.navigateTo({
      url: '../my/my-faultRepair/my-faultRepair',
    })
  },
  closeModal: function () {
    var that = this;
    that.setData({
      showModal: false
    })
  },
  closeNewTip: function () {
    var that = this;
    that.setData({
      newTip: false
    })
    wx.removeStorageSync("isOk")
  },
  goNewActive: function () {
    wx.navigateTo({
      url: '../my/my-newActivity/my-newActivity',
    })
    var that = this;
    that.setData({
      newTip: false
    })
    wx.removeStorageSync("isOk")
  }
})
